﻿using System;
using System.Drawing;
using System.Windows.Forms;
using Microsoft.WindowsMobile.Samples.Location;
using IRQ.BaseFunctions;

namespace SimpleGPSApp.Controls
{
    public partial class SatellitesView : UserControl
    {
        Font mainFont = new Font(FontFamily.GenericSansSerif, 8.25f, FontStyle.Regular);
        Brush greenBrush = new SolidBrush(Color.Green);
        Brush redBrush = new SolidBrush(Color.Red);
        Brush blackBrush = new SolidBrush(Color.Black);
        Brush activeBrush = null;
        Pen greenPen = new Pen(Color.Green);
        Pen blackPen = new Pen(Color.Black);
        Bitmap buffer;

        private int outerCircleDiameter = 0;
        private int middleCircleDiameter = 0;
        private int innerCircleDiameter = 0;
        int outerRadius = 0;
        int middleRadius = 0;
        int innerRadius = 0;
        private int centerX = 0;
        private int centerY = 0;

        private GpsPosition position = null;

        public GpsPosition Position
        {
            set
            {
                position = value;
                if (position != null)
                {
                    this.Refresh();
                }
            }
        }

        public SatellitesView()
        {
            InitializeComponent();
        }

        private void DrawCircles(Graphics graphics)
        {
            centerX = Height / 2;
            centerY = Width / 2;
            int step = 0;
            if (Width <= Height)
            {
                SetValues(Width, ref step);
            }
            else
            {
                SetValues(Height, ref step);
            }

            outerRadius = outerCircleDiameter / 2;
            middleRadius = middleCircleDiameter / 2;
            innerRadius = innerCircleDiameter / 2;

            int centeXMinusOuterRadius = centerX - outerRadius;
            int centeYMinusOuterRadius = centerY - outerRadius;
            graphics.DrawEllipse(greenPen, new Rectangle(centeYMinusOuterRadius, centeXMinusOuterRadius, outerCircleDiameter, outerCircleDiameter));
            graphics.DrawEllipse(greenPen, new Rectangle(centerY - middleRadius, centerX - middleRadius, middleCircleDiameter, middleCircleDiameter));
            graphics.DrawEllipse(greenPen, new Rectangle(centerY - innerRadius, centerX - innerRadius, innerCircleDiameter, innerCircleDiameter));

            graphics.DrawLine(greenPen, centerY, centerX + outerRadius, centerY, centeXMinusOuterRadius);
            graphics.DrawLine(greenPen, centerY + outerRadius, centerX, centeYMinusOuterRadius, centerX);

            mainFont = new Font(FontFamily.GenericSansSerif, 0.5f * step, FontStyle.Regular);
            
            float twoTenthStep = 0.2f * step;
            float forTenthStep = 0.4f * step;

            graphics.DrawString("N", mainFont, greenBrush, centerY - twoTenthStep, centeXMinusOuterRadius - 0.7f * step);
            graphics.DrawString("W", mainFont, greenBrush, centeYMinusOuterRadius - 0.8f * step, centerX - forTenthStep);
            graphics.DrawString("E", mainFont, greenBrush, centerY + outerRadius + forTenthStep, centerX - 0.3f * step);
            graphics.DrawString("S", mainFont, greenBrush, centerY - twoTenthStep, centerX + outerRadius + twoTenthStep);
        }

        private void SetValues(int size, ref int step)
        {
            step = (int)Math.Ceiling(0.1 * size);
            outerCircleDiameter = size - 2 * step;
            middleCircleDiameter = size - 3 * step;
            innerCircleDiameter = size - 5 * step;
        }

        // satellites drawing space
        private void DrawSatellites(Graphics g)
        {
            if (position != null)
            {
                foreach (Satellite sat in position.GetSatellitesInView())
                {
                    double azimuth = IRQMath.ToRadians(sat.Azimuth);
                    float x = (outerRadius * sat.Elevation) / 90;
                    float y = x * (float)Math.Sin(azimuth);
                    x += centerX;
                    y += centerY;

                    activeBrush = position.SatelliteIsInSolution(sat) ? blackBrush : redBrush;
                    g.DrawString(sat.Id.ToString(), mainFont, activeBrush, x, y);
                }
            }
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);
            this.buffer = new Bitmap(this.Width, this.Height);

            using (Graphics graphics = Graphics.FromImage(buffer))
            {
                graphics.Clear(this.BackColor);

                DrawCircles(graphics);
                DrawSatellites(graphics);
            }
            e.Graphics.DrawImage(buffer, 0, 0);
        }

        protected override void OnPaintBackground(PaintEventArgs e)
        {
        }
    }
}
